package com.gadgets.cm01.auth.controller.impl;

import com.gadgets.cm01.auth.controller.IAuthorityController;
import com.gadgets.cm01.auth.dto.AuthorityQueryDTO;
import com.gadgets.cm01.auth.dto.AuthorityTreeDTO;
import com.gadgets.cm01.auth.pojo.Authority;
import com.gadgets.cm01.auth.service.IAuthorityService;
import com.gadgets.framework.annotation.RoleAuthCheck;
import com.gadgets.framework.common.constant.GadgetsConstant;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-09-29
 *
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/authority")
public class AuthorityControllerImpl implements IAuthorityController {

    @Autowired
    @Qualifier(value = "authorityServiceImpl")
    private IAuthorityService authorityService;

    /**
     * 根据父菜单ID查询菜单树
     *
     * @param request  请求信息
     * @param response 应答信息
     * @param queryDTO 查询条件
     *
     * @return 查询结果
     */
    @Override
    @RoleAuthCheck(ifLogin = true)
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryAuthTree.do")
    public AuthorityTreeDTO queryAuthorityTree(HttpServletRequest request, HttpServletResponse response, AuthorityQueryDTO queryDTO) {
        queryDTO.setIfGetTree(GadgetsConstant.NUMBER_CONSTANT.STR_NUMBER_1);
        queryDTO.setAuthId(queryDTO.getpAuthId());
        List<String> orderColumns = new ArrayList<>();
        orderColumns.add("ID");
        orderColumns.add("AUTH_ORDER");
        queryDTO.setOrderColumn(orderColumns);
        return authorityService.queryAndBuildAuthorityTree(queryDTO);
    }

    /**
     * 分页条件查询菜单列表
     *
     * @param request    请求信息
     * @param response   应答信息
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     */
    @Override
    @RoleAuthCheck(userType = {GadgetsConstant.USER_TYPE.SYS_MANAGER})
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryAuthList.do")
    public HttpBaseResp<Authority> queryAuthorityInfoCondition(HttpServletRequest request, HttpServletResponse response, AuthorityQueryDTO queryDTO, Pagination pagination) {
        List<Authority> list = authorityService.queryAuthorityInfoByPagination(queryDTO, pagination);
        return new HttpBaseResp<>(list, pagination);
    }

}
